package com.scope.customViews;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.scope.R;
import com.scope.tasks.OnSwipeTouchListener;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Samrith on 11/1/13.
 */
public class ImageSlider extends FrameLayout {
    public ImageSlider(Context context) {
        super(context);
        mContext = context;
        mImagePaths = new ArrayList<String>();
        initializeViews();
        setBackgroundColor(Color.argb(230, 130, 130, 130));
    }

    private Context mContext;
    private ImageRotatedView mPrevImageView;
    private ImageRotatedView mNextImageView;
    private ImageRotatedView mMainImageView;
    private List<String> mImagePaths;

    private int mPosition;
    public int getPosition() {
        return mPosition;
    }

    public void setPosition(int pos) {
        mPosition = pos;
        setImages();
    }

    public final static int INVALID_POSITION = -1;

    public void setProperty(Collection<String> path) {
        setProperty(path, INVALID_POSITION);
    }

    public void setProperty(Collection<String> path, int position) {
        mImagePaths.clear();
        mImagePaths.addAll(path);
        mPosition = position;
        setImages();
    }

    private Drawable getDrawable(int pos) {
        try {
            return Drawable.createFromPath(mImagePaths.get(pos));
        } catch(Exception e) {
            return DFLT_DRAWABLE;
        }
    }

    public final Drawable DFLT_DRAWABLE = getResources().getDrawable(R.drawable.icon_image);

    public void setImages() {
        Drawable prev = getDrawable(mPosition - 1);
        mPrevImageView.setImageDrawable(prev);

        Drawable next = getDrawable(mPosition + 1);
        mNextImageView.setImageDrawable(getDrawable(mPosition + 1));

        mMainImageView.setImageDrawable(getDrawable(mPosition));
    }

    private void initializeViews() {
        CustomView.FreqLog.InitializedViews(this);
        mPrevImageView = new ImageRotatedView(mContext);
        mPrevImageView.setScreenAlpha(120);
        mPrevImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPosition > 0) {
                    setPosition(mPosition - 1);
                }
            }
        });

        mNextImageView = new ImageRotatedView(mContext);
        mNextImageView.setScreenAlpha(120);
        mNextImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPosition < mImagePaths.size() - 1) {
                    setPosition(mPosition + 1);
                }
            }
        });

        mMainImageView = new ImageRotatedView(mContext);
        mMainImageView.setBackgroundColor(Color.argb(130, 255, 255, 255));
        mMainImageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
            @Override
            public void onSwipeTop() {
                mNextImageView.performClick();
            }

            @Override
            public void onSwipeLeft() {
                mNextImageView.performClick();
            }

            @Override
            public void onSwipeBottom() {
                mPrevImageView.performClick();
            }

            @Override
            public void onSwipeRight() {
                mPrevImageView.performClick();
            }

            @Override
            public void onSingleTap() {
                performClick();
            }
        });

        addView(mPrevImageView);
        addView(mNextImageView);
        addView(mMainImageView);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        resizeViews(w, h);
    }

    private static LayoutParams getLayoutParams(int width, int height, int marginWidth, int marginHeight) {
        LayoutParams lp = new LayoutParams(width, height);
        lp.setMargins(marginWidth, marginHeight, marginWidth, marginHeight);
        return lp;
    }

    private void resizeViews(int width, int height) {
        CustomView.FreqLog.PositioningViews(this);
        int left, top, right, buttom;
        int layoutWidth, layoutHeight;

        //set MainImageView
        if (width > height) {
            layoutWidth = (int)(MID_SHARE * width);
            left = (width - layoutWidth) / 2;
            top = (int)(MARGIN * height);
            layoutHeight = height - 2 * top;
        } else {
            layoutHeight = (int)(MID_SHARE * height);
            top = (height - layoutHeight) / 2;
            left = (int)(MARGIN * width);
            layoutWidth = width - 2 * left;
        }
        LayoutParams mainLP = new LayoutParams(layoutWidth, layoutHeight);
        mainLP.setMargins(left, top, left, top);
        mMainImageView.setLayoutParams(mainLP);

        //set PrevImageView
        if (width > height) {
            buttom = top;
            right = left + layoutWidth;
            left = left - layoutWidth;
        } else {
            right = left;
            buttom = top + layoutHeight;
            top = top - layoutHeight;
        }
        LayoutParams prevLP = new LayoutParams(layoutWidth, layoutHeight);
        prevLP.setMargins(left, top, right, buttom);
        mPrevImageView.setLayoutParams(prevLP);

        //set NextImageView
        if (width > height) {
            buttom = top;
            right = left - 2 * layoutWidth;
            left = left + 2 * layoutWidth;
        } else {
            right = left;
            buttom = top - 2 * layoutHeight;
            top = top + 2 * layoutHeight;
        }
        LayoutParams nextLP = new LayoutParams(layoutWidth, layoutHeight);
        nextLP.setMargins(left, top, right, buttom);
        mNextImageView.setLayoutParams(nextLP);

        if (width > height) {
            mPrevImageView.setAngle(0f, -ANGLE, 0f);
            mNextImageView.setAngle(0f, ANGLE, 0f);
        } else {
            mPrevImageView.setAngle(ANGLE, 0f, 0f);
            mNextImageView.setAngle(-ANGLE, 0f, 0f);
        }
        CustomView.FreqLog.PositionedViews(this);
    }

    private final static float ANGLE = 30f;
    private final static float MARGIN = 0.03f;
    private final static float MID_SHARE = 0.62f;
}
